package cc.openkit.admin.controller.shop_brand;

import cc.openkit.admin.model.*;
import cc.openkit.admin.service.g.GGroupLimitService;
import cc.openkit.admin.service.shopBrand.ShopBrandService;
import cc.openkit.admin.service.shopType.ShopTypeService;
import cc.openkit.admin.service.sign.SignService;
import cc.openkit.admin.util.AppUtil;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
@RequestMapping("/sbc")
public class ShopBrandController {
    private Logger log = Logger.getLogger(ShopBrandController.class);

    @Resource
    private ShopBrandService shopBrandService;
    @Resource
    private GGroupLimitService gGroupLimitService;
    @Resource
    private ShopTypeService shopTypeService;

    /**
     * 添加的方法
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public ModelAndView getAll(HttpServletRequest request){

        // 权限验证。这里的最后的 5 就是上面我们找到菜单的ID，需要在这里查找
        GGroupLimit gGroupLimit = gGroupLimitService.testGroup(request, 29);

        ModelAndView mv = new ModelAndView();

        // 没有权限，返回错误页面
        if(gGroupLimit==null){
            mv.setViewName("/admin/err/no_group_err");
            mv.addObject("msg", StaticFinalVar.NO_GROUP_MSG+ StaticFinalVar.CALL_GROUP);
            return mv;
        }

        // 取值
        mv.setViewName("/admin/shop_brand/list"); // 需要跳转的页面
        mv.addObject("kitG",gGroupLimit);
        return mv;
    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "/getAllJson", method = RequestMethod.POST)
    @ResponseBody
    public Object getAllJson(HttpServletRequest request)throws Exception{
        log.info("获取品牌数据");

        Map<String,Object> map = new HashMap<String,Object>();
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数
        String limit = request.getParameter("limit");// 获得每页显示条数
        String search = request.getParameter("search");// 获取搜索条件

        // 封装数据
        ShopBrand shopBrand=new ShopBrand();
        if(KitUtil.feikong(search)){
            shopBrand.setSbNameCn(search);
        }

        // 分页查询  //查询所有品牌列表
        List<ShopBrand> shopBrands = shopBrandService.findModelPageListByWhere(shopBrand, Integer.valueOf(page), Integer.valueOf(limit));
        // 获取查询到的总数
        int size = shopBrandService.queryCount(shopBrand);

        // 返回数据
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",shopBrands);

        return JSONObject.toJSON(map);
    }

    /**
     * 去添加页面
     * @param request
     * @return
     */
    @RequestMapping(value = "/goAdd", method = RequestMethod.GET)
    public Object goAdd(HttpServletRequest request){
        log.info("品牌添加 》 添加 》 跳转");
        //查询出所有的分类 给某个分类进行添加品牌
        List<ShopType> shopType1 = shopTypeService.findShopType();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/shop_brand/add");
        mv.addObject("kitG",shopType1);
        return mv;
    }

    /**
     * 添加
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(HttpServletRequest request){
        log.info("品牌列表 》 添加 》 保存");
        String shopTypeStr = request.getParameter("shopTypeStr");//品牌分类
        String sbLogoUrl = request.getParameter("sbLogoUrl");//品牌logo
        String sbNameCn = request.getParameter("sbNameCn");//品牌中文名
        String sbNameEn = request.getParameter("sbNameEn");//品牌英文名
        String sbWeb = request.getParameter("sbWeb");//品牌官网
        String sbType = request.getParameter("sbType");//品牌状态
        String sbDescribe = request.getParameter("sbDescribe");//品牌描述
        String sbOldPractice = request.getParameter("sbOldPractice");//品牌故事

        ShopBrand shopBrand=new ShopBrand();
        if(shopTypeStr!=null&&!"".equals(shopTypeStr)){
            shopBrand.setStId(shopTypeStr);
        }
        if(sbLogoUrl!=null&&!"".equals(sbLogoUrl)){
            shopBrand.setSbLogoUrl(sbLogoUrl);
        }
        if(sbNameCn!=null&&!"".equals(sbNameCn)){
            shopBrand.setSbNameCn(sbNameCn);
        }
        if(sbNameEn!=null&&!"".equals(sbNameEn)){
            shopBrand.setSbNameEn(sbNameEn);
        }
        if(sbWeb!=null&&!"".equals(sbWeb)){
            shopBrand.setSbWeb(sbWeb);
        }
        if(sbType!=null&&!"".equals(sbType)){
            shopBrand.setSbType(Integer.parseInt(sbType));
        }
        if(sbDescribe!=null&&!"".equals(sbDescribe)){
            shopBrand.setSbDescribe(sbDescribe);
        }
        if(sbOldPractice!=null&&!"".equals(sbOldPractice)){
            shopBrand.setSbOldPractice(sbOldPractice);
        }
        shopBrand.setSbFoundTime(new Date());
        shopBrand.setSbId(KitUtil.uuid());

        return JSONObject.toJSON(shopBrandService.saveSelect(shopBrand)==1 ? KitUtil.returnMap("200",StaticFinalVar.ADD_OK) : KitUtil.returnMap("101",StaticFinalVar.ADD_ERR));
    }

    /**
     * 修改前的查询
     * @param request
     * @return
     * @throws Exception
//     */
    @RequestMapping(value = "/goUpdate", method = RequestMethod.GET)
    public ModelAndView goUpdate(HttpServletRequest request) throws Exception{
        log.info("品牌管理 》 修改 》 跳转");
        List<ShopType> shopTypeModel = shopTypeService.findShopType();
        String id = request.getParameter("id");
        ShopBrand modelById = shopBrandService.findModelById(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/shop_brand/update");
        mv.addObject("kitModel",modelById);
        mv.addObject("shopTypeModel",shopTypeModel);
        return mv;
    }

    /**
     * 修改
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request){
        log.info("品牌列表 》 编辑 》 保存");
        String sbId = request.getParameter("sbId");
        String shopTypeStr = request.getParameter("shopTypeStr");//品牌分类
        String sbLogoUrl = request.getParameter("sbLogoUrl");//品牌logo
        String sbNameCn = request.getParameter("sbNameCn");//品牌中文名
        String sbNameEn = request.getParameter("sbNameEn");//品牌英文名
        String sbWeb = request.getParameter("sbWeb");//品牌官网
        String sbType = request.getParameter("sbType");//品牌状态
        String sbDescribe = request.getParameter("sbDescribe");//品牌描述
        String sbOldPractice = request.getParameter("sbOldPractice");//品牌故事

        ShopBrand shopBrand = shopBrandService.queryByUUID(sbId);
        if(shopBrand!=null&&!"".equals(shopBrand)){
            if(shopTypeStr!=null&&!"".equals(shopTypeStr)){
                shopBrand.setStId(shopTypeStr);
            }
            if(sbLogoUrl!=null&&!"".equals(sbLogoUrl)){
                shopBrand.setSbLogoUrl(sbLogoUrl);
            }
            if(sbNameCn!=null&&!"".equals(sbNameCn)){
                shopBrand.setSbNameCn(sbNameCn);
            }
            if(sbNameEn!=null&&!"".equals(sbNameEn)){
                shopBrand.setSbNameEn(sbNameEn);
            }
            if(sbWeb!=null&&!"".equals(sbWeb)){
                shopBrand.setSbWeb(sbWeb);
            }
            if(sbType!=null&&!"".equals(sbType)){
                shopBrand.setSbType(Integer.parseInt(sbType));
            }
            if(sbDescribe!=null&&!"".equals(sbDescribe)){
                shopBrand.setSbDescribe(sbDescribe);
            }
            if(sbOldPractice!=null&&!"".equals(sbOldPractice)){
                shopBrand.setSbOldPractice(sbOldPractice);
            }
            Integer integer = shopBrandService.updateSelective(shopBrand);
            if(integer<0){
               return KitUtil.returnMap("101",StaticFinalVar.UPDATE_ERR);
            }else{

            }
        }else{
            return KitUtil.returnMap("101","修改失败");
        }
        return KitUtil.returnMap("200",StaticFinalVar.UPDATE_OK);
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Object del(HttpServletRequest request){
        log.info("品牌 》 删除");
        String id = request.getParameter("id");
        return shopBrandService.deleteByUUId(id)>0 ? KitUtil.returnMap("200",StaticFinalVar.DEL_OK) : KitUtil.returnMap("101",StaticFinalVar.DEL_ERR);
    }

}
